using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using $safeprojectname$.Tools;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace $safeprojectname$.MVC
{
    public class BaseHttpApplication : System.Web.HttpApplication
    {


        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            HttpException httpException = exception as HttpException;

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        Uri url = Request.Url;
                        Log404Error(url.ToString());
                        if (!url.IsPicture() && !url.IsCss() && !url.IsJs())
                        {
                            // Log the exception.
                            Response.Clear();
                            RouteData routeData = new RouteData();
                            routeData.Values.Add("controller", "Error");
                            // Page not found.
                            routeData.Values.Add("action", "HttpError404");
                            // Pass exception details to the target error View.
                            routeData.Values.Add("error", exception);
                            // Pass exception details to the target error View.
                            routeData.Values.Add("filePath", url.ToString());
                            // Clear the error on server.
                            Server.ClearError();

                            // Avoid IIS7 getting in the middle
                            Response.TrySkipIisCustomErrors = true;

                            // Call target Controller and pass the routeData.
                            IController errorController = GetErrotController();
                            errorController.Execute(new RequestContext(
                                 new HttpContextWrapper(Context), routeData));
                        }
                        break;

                    // Here you can handle Views to other error codes.
                    // I choose a General error template  
                    default:
                        break;
                }
            }
        }

        private void Log404Error(string missingFile)
        {
            IDictionary<string, object> properties = new Dictionary<string, object>();
            properties.Add("DateTime", DateTime.Now);
            properties.Add("DateTime Formated", DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
            properties.Add("Missing file", missingFile);
            properties.Add("Referrer file", Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : "");
            Logger.Write("404", "General", 1, 1, TraceEventType.Error, "Missing File", properties);
        }

        public virtual IController GetErrotController()
        {
            throw new NotImplementedException("GetErrotController must be override");
        }
    }
}
